home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: MegaDisc / MegaDisc 08 (1988)(MegaDisc Digital Publishing)(AU)[WB].zip / MegaDisc 08 (1988)(MegaDisc Digital Publishing)(AU)[WB].adf / ARTICLES / multitasking < prev    next >
Text File  |  1988-05-28  |  17KB  |  303 lines

  1.  
  2.  
  3.           MULTITASKING AND THE AMIGA
  4.                                           by Peter McKellar
  5.  
  6.    [Ed: Peter is a Consultant Systems Programmer in large IBM mainframe
  7.        environments. He's also put together a MultiTasking Theme Disk
  8.        pulling together material from various sources on this very
  9.        important topic, including Pete Goodeve's IPC standards - see the
  10.        end of the article. Many thanks, Pete.]
  11.  
  12.  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  13.  
  14.     People are often confused by the term "multitasking". This article will
  15.  attempt to explain some terms for the novice.  If you are an experienced
  16.  user there may also be a few points for you.
  17.  
  18.          Multitasking Comparison.
  19.  It doesn't take a genius to see that the Amiga is a far superior machine
  20.  to an IBM PC or clone.  We all know that an Amiga is better than a MAC or
  21.  ATARI, both on price and performance.  But what is it that makes the Amiga
  22.  really shine? Obviously, its multitasking capability alone makes it stand
  23.  out from the opposition.  PC users claim that "MEMORY RESIDENT" programs
  24.  permit them to run more than one thing at a time.  MAC users also claim
  25.  that their machines permit more than one program to run at a time.  The
  26.  ATARI user, whilst closest of all, claims multiple execution is possible.
  27.  This is not true.  Multitasking, in the full meaning of the word, does not
  28.  occur.
  29.  
  30.  None of these others can boast the power of the Amiga's true multitasking
  31.  environment.  The term, "memory resident", simply means that a program has
  32.  been preloaded into memory.  This means that it is occupying memory, lying
  33.  dormant until branched to by a key-stroke. When this occurs the previously
  34.  executing program loses control until another key-stroke combination is
  35.  used.  An apt analogy would be to a large program with procedures that are
  36.  branched to when certain keys are hit.  The only difference is that various
  37.  parts of the program are sold by different suppliers.  Memory management
  38.  becomes a problem when so many users want to share only 640K.  All the pro-
  39.  grammers must agree upon where their code will reside.  The unfortunate
  40.  truth is that most suppliers don't.  System crashes result when programs
  41.  don't agree.  You often see suppliers giving the cryptic instructions:
  42.  
  43.                            "LOAD ME LAST"
  44.  
  45.  The basis for this is to ensure that memory doesn't get corrupted and that
  46.  branch vectors are installed correctly.
  47.  
  48.  The MAC also relinquishes control of the CPU to another task when the user
  49.  "clicks" on another icon, thereby selecting it for execution.  Everything
  50.  else stops while the currently active task takes over, even if this task
  51.  is awaiting input from the keyboard or serial port, or patiently sitting
  52.  back while your slow old printer grinds out another line or two from the
  53.  buffer.
  54.  
  55.  The ATARI is similar to the AMIGA in that multiple tasks can coexist and it
  56.  does share some of the CPU cycles throughout different functions.  This is
  57.  done on a fixed time, rotational, basis.  But tasks don't have a priority
  58.  based scheduling system like the AMIGA.  I haven't undertaken a study of
  59.  the ATARI, but I believe that the AMIGA supports a far more extensive lot
  60.  of operating system calls.  I can't deny it - I'm just an AMIGA BIGOT!!!
  61.  
  62.  In theory, the new OS/2 system, for IBM PCs does offer some sophisticated
  63.  multitasking capabilities.  But let's face it, there isn't any software
  64.  around that uses any of the features.  It costs a fortune and won't run on
  65.  less than one and a half meg.  That's the first release of OS/2, the full
  66.  blown version, with graphics and all, will require 4 megabytes - just for
  67.  the operating system (pretty hot, eh?).  The full-featured version is not
  68.  yet available.  It's actually tied up in litigation from APPLE, and nobody
  69.  is writing software for it until the court case is decided. As the case is
  70.  simply a stalling action brought by APPLE, to cripple IBM's release of
  71.  OS/2, it could possibly be dragged on for years.  In the meantime, this
  72.  is an opportunity for the AMIGA to slip through and top sales over both.
  73.  
  74.  The AMIGA operating system is far superior to all of the systems now avail-
  75.  able.  Its system displays the same techniques of multitasking that can be
  76.  seen on high-end work stations right through to large mainframes.  The only
  77.  real difference lies in the ability to recover from catastrophic errors.
  78.  The AMIGA Operating System is likely to invoke Mr. Guru if you don't follow
  79.  the rules properly.
  80.  
  81.  MULTITASKING OVERVIEW.
  82.  As microprocessors become faster and faster, multitasking becomes more and
  83.  more important.  Even with the superb custom chips installed in the AMIGA,
  84.  I/O speed is a major contraint.  It is wasteful for the CPU to be idle as
  85.  it waits for input or output operations to complete.  Any service that
  86.  relies on some physical action (eg driving the electron guns that display
  87.  text and graphics on the screen, or reading a disk) takes thousands of
  88.  times longer to complete than the execution of programs within the CPU.
  89.  It even takes longer to execute instructions that operate on RAM, rather
  90.  than the data registers located on the microprocessor chip.
  91.  
  92.  Multitasking allows the processor to INITIATE one of these time-consuming
  93.  activities (eg write to the disk) then promptly forget about it.  It can
  94.  now move on to more productive activities, like playing a game, calculating
  95.  pi to a million places, compiling a program, balancing your accounts and
  96.  any other tasks that require your attention and computing power.  Each
  97.  time one of these other tasks no longer requires CPU cycles, someone else
  98.  gets a chance to use the processor.  More important tasks may also gain
  99.  access to the CPU by forcing an interrupt.
  100.  
  101.  But how does the Operating System achieve such harmony, such a blissful
  102.  and orderly sharing of resources?  By careful design and hardware support.
  103.  Commodore have a priority-based task dispatching technique.  This is
  104.  possible only if the software is well written and documented so the user
  105.  (in this case, the programmer) can access required services (eg, read).
  106.  The hardware must also permit interrupts, which stop the currently active
  107.  task and allow the Operating System to reassign control of the CPU to a
  108.  more deserving task.  The ability to interrupt a task makes this a
  109.  "pre-emptive" Operating System.
  110.  
  111.  Only a task with a higher priority than the currently executing task can
  112.  usurp control from the present task.  Tasks in the system with the same
  113.  priority as that currently executing will still get to use the processor.
  114.  Tasks of the SAME priority get to equally share CPU on a rotational basis.
  115.  The operating system forces a time interval interrupt to ensure that one
  116.  task doesn't hog the chip.
  117.  
  118.  If it wasn't for multitasking, the CPU would be idle for the greater part
  119.  of its existence.  Multitasking allows the user to regain most of the power
  120.  otherwise lost.  When higher priority tasks are waiting for resources other
  121.  than CPU, lower priority tasks can execute. They will be interrupted and
  122.  put on hold by those higher priority tasks when their wait condition is no
  123.  longer outstanding (eg their I/O has completed).  When the higher priority
  124.  task again waits, the lower priority task again gains control.  The same
  125.  happens time and time again.
  126.  
  127.  INTERRUPTS.
  128.  The term "interrupt" has been mentioned several times so far.  Maybe I need
  129.  to clarify just what an interrupt is, and how they work.  Interrupts are
  130.  first supported in the hardware of the Motorola 68000 microprocessor chip.
  131.  The 68000 recognises any number of interrupts, but only seven priority
  132.  levels are recognised.  Priority becomes important when multiple interrupts
  133.  are in effect.  An example of when this will occur is when an interrupt is
  134.  received from an external device (say a disk drive indicating it is ready
  135.  to transmit data) and a program has tried to do a divide by zero.  The
  136.  system must be able to distinguish who to service first, the program or the
  137.  drive.  The Amiga's Operating System provides further discrimination with
  138.  interrupts.  In conjuction with the Peripheral Interface Adapter chip, (the
  139.  8520), and one of the custom chips, the 4703, up to 16 interrupt levels are
  140.  provided.  The table below shows these.
  141.  
  142.     4703         CPU          Pseudo
  143.     Name         Priority     Priority    Purpose
  144.     NMI          7            15          Non-Maskable Interrupt
  145.     INTEN        6            14          Special (Copper)
  146.     EXTERN       6            13          8520B, external level 6
  147.     DSKSYNC      5            12          Disk Byte
  148.     RBF          5            11          Serial Input
  149.     AUD1         4            10          Audio Channel 1
  150.     AUD3         4            9           Audio Channel 3
  151.     AUD0         4            8           Audio Channel 0
  152.     AUD2         4            7           Audio Channel 2
  153.     BLIT         3            6           Blitter Done
  154.     VERTB        3            5           Vertical Blank
  155.     COPER        3            4           Copper
  156.     PORTS        2            3           8520A, external level 2
  157.     TBE          1            2           Serial Output
  158.     DSKBLK       1            1           Disk Block Done
  159.     SOFTINT      1            0           Software Interrupts
  160.  
  161.  In our example, the standard CPU's seven priorities could have resolved the
  162.  conflict.  A DSKSYNC would have been received at priority "5" and the zero
  163.  divide would have generated a level 1 priority interrupt, ie SOFTINT.  But
  164.  what if the COPPER and the BLITTER both generated an interrupt during a
  165.  vertical blanking period interrupt?  Purists would recognise that this is
  166.  an improbable, if not impossible situation, but serves only as an example.
  167.  It does however demonstrate the neccessity of the extended interrupt
  168.  priorities provided by the AMIGA's Operating System.
  169.  
  170.  DISPATCHING.
  171.  Now that we realise what the custom chip's role is in assigning priorities,
  172.  who will get dispatched first ? A task can be in one of 6 states , they
  173.  are :
  174.      1. Running
  175.      2. Ready for execution
  176.      3. Waiting on an external event
  177.      4. Just added to the ready queue
  178.      5. Being removed from the Task queue
  179.      6. Awaiting special exception processing
  180.  Items 4,5 and 6 are transient states and will not be discussed , The system
  181.  maintains 2 queues of tasks that are not presently running. The first is
  182.  sorted on priority and contains "ready tasks", those available to be
  183.  dispatched.
  184.  
  185.  The second queue is a list of all tasks awaiting an external event (eg the
  186.  completion of I/0). These are not held in priority order.  When an
  187.  interrupt is generated (for example,when the I/O completes to a device),
  188.  the queues are re-examined.  The task that was on the "not ready" queue,
  189.  but whose I/O just completed, is requeued onto the "ready tasks" queue.
  190.  This queue is then scanned from the head of the queue.  As this queue is
  191.  sorted into priority order, the highest priority task will be found first.
  192.  This will be dispatched.  The exception to this comes when a pre-specified
  193.  "quantum" of time has expired.  When this happens, the next task of the
  194.  same priority gets a chance to execute.  This time-slicing will continue,
  195.  amongst the highest priority tasks, until a higher priority task is
  196.  placed at the head of the "ready tasks" queue.
  197.  
  198.  Priorities can range between -128 (the lowest) to +127 (the highest).
  199.  System tasks vary between -20 and +20. Zero priority should be used by
  200.  user applications unless a special interaction with system tasks is
  201.  required.
  202.  
  203.  USING MULTITASKING
  204.  Multitasking can fall into one of 3 broad catergories.
  205.      1. Stand alone processes
  206.      2. Processes generated by another process
  207.      3. Tasks generated by processes or other tasks
  208.  
  209.  STAND ALONE PROCESSES are what you generate if you run a batch file or
  210.  command under CLI, or cause another window to be opened under WORKBENCH.
  211.  A more common example would be having a clock active in your title bar,
  212.  doing a background print of a file, and doing a directory list to your
  213.  console.  This serialization of resouces may also require the program to
  214.  wait (eg wait for keyboard input, letter by letter, of a command).  More
  215.  waits will occur if both your directory listing, and your file print, need
  216.  to use the disk drive at the same time.  This is a far more efficient way
  217.  to use your machine than if you were to do each task in turn.  The actual
  218.  elapsed time will be much less - who says there's no such thing as a free
  219.  lunch?  I guess the only real cost is in CPU cycles required to service
  220.  the dispatching cycle, maintaining the queues, and serializing access to
  221.  physical devices like the disk drives.  These are all pretty meager costs
  222.  compared to the savings.
  223.  
  224.  The more observant readers will have noticed that I used two terms before,
  225.  TASK and PROCESS.  These are both features of multitasking but they work
  226.  at different levels of the operating system.  Tasks work at a much lower
  227.  level that Processes.
  228.  
  229.  Tasks are unable to perform DOS functions (eg producing output) and so a
  230.  process may have to spawn another process if these facilities are required.
  231.  The difference lies with a process being created through DOS, whereas a task
  232.  is one level lower, being created by EXEC.  Consequently, if you want a
  233.  technical reference for setting up a Process, you refer to the AmigaDOS
  234.  Manual, put out by BANTAM.  The section of this manual titled "AmigaDOS
  235.  Developer's Manual" (bottom of page 189 in my copy) will show you the
  236.  syntax of a "LoadSeg" call, over the page is the "UnLoadSeg" call.  These
  237.  calls load and unload segments of code for the express purpose of
  238.  spawning new Processes.  The address of the code just loaded is referred
  239.  to in the "CreateProc" call (page 187).
  240.  
  241.  Tasks are explained in the "ROM Kernel Reference Manual: Exec".  The two
  242.  calls AddTask and RemTask are used to create tasks at the Exec level.  The
  243.  calls rely on the correct initialization being performed.  All necessary
  244.  information can be found in the Exec manual, and this is illustrated with
  245.  many examples.  But if you're the lazy type like me, this won't be
  246.  sufficient.  I NEED code, in machine readable format (ie on disk), and I
  247.  have to know that it works.  For this reason I have scoured the FISH disks
  248.  and found some interesting material.  Megadisc Theme disks are currently
  249.  in preparation, specifically demonstrating task and process creation.
  250.  Existing public domain offerrings are possibly a little cryptic, and don't
  251.  always deliver what they promise.  I will be reworking these into a format
  252.  suitable for distribution and append relevant documentation.  Megadisc will
  253.  notify its readers when the disks become available.
  254.  
  255.  Follow-on articles will provide relevant code, comprehensively annotated,
  256.  so that you too can write multitasking programs!!
  257.  
  258.  IPC
  259.  On a slightly different track, I have just received word from Pete Goodeve
  260.  (of XICON fame) in the USA.  He has been the leading light and inspiration
  261.  for a new standard - "IPC".  This new and mysterious acronym is short for
  262.  "Inter Process Communication". This standard will do for multitasking what
  263.  IFF did for files.  The standard will permit different modules to talk a
  264.  common language provided that all agree to follow the same conventions.
  265.  
  266.  But, you ask, why?  The possibilities are limitless.  It would be possible
  267.  to mix'n'match commercial packages and public domain software.  The word
  268.  processor of choice with your favourite spelling checker, a different
  269.  printing package, and then send it all by modem to a friend - but all by
  270.  the one program.  External interfaces with a database retrieval system
  271.  could be all powerful.  Real time data capture by slave processes would
  272.  be simple, your comms package could quietly lift data off a videotex
  273.  system, pass it to one of a number of slave processes, depending on
  274.  content.  Key values could be plugged into your spreadsheet: - by now
  275.  you're starting to see the awesome power of such a standard??!!
  276.  
  277.  Pete and his co-workers had posted this standard on USENET in the USA and
  278.  after much debate its form has stabilised.  The very first copy of the
  279.  sample disk arrived in my letterbox only days ago, along with three
  280.  catalogues and two bills.  No doubt you'll only be interested in the
  281.  sample disk, so I've forwarded this to Megadisc, for packaging as a theme
  282.  disk.
  283.  
  284.  If sufficient interest is shown, I will write some more on the standard in
  285.  a future issue of Megadisc.  I suspect that we will hear far more about
  286.  this new standard in the coming months.
  287.  
  288.  [Ed: Any programmer out there who is interested in this IPC development
  289.  and wants to get Pete's disk, just call on (02) 9593692. This initiative
  290.  looks like it could out-hype HyperCard on the Mac, but like anything that
  291.  starts, it needs support, especially from other countries. So anyone who
  292.  needs a project, here it is. And Pete Goodeve is a most deserving person,
  293.  not to mention capable - a bit of gossip, he tells me that WorkBench 1.3
  294.  featured a XICON lookalike, with no attribution to him. After presenting
  295.  his case to Commodore, they decided to change the name to ICONX...And
  296.  anyway, it doesn't work on the 1.3 currently available. Anyway we
  297.  continue to use the excellent XICON and are grateful to Pete for the
  298.  support he showed from the very beginning.]
  299.  
  300. XXXXXXXXXXXXXXXXXXXXXXXXXXXX END OF MULTI-TASKING XXXXXXXXXXXXXXXXXXXXXXXXX
  301.  
  302.  
  303.